<?php

# PMB Online
# Aplikasi PMB Online untuk Pendaftaran Calon Mahasiswa di PT/PTS Indonesia
# 
# version 1.0
# class gelombang
# file models/gelombang.php
# created Jan 22, 2013 3:22:39 PM
# 
# (c)2012, arif.laksito@gmail.com

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class gelombang extends SI_Model {

    var $data = array();

    public function __construct() {
        parent::__construct();
    }
    
    public function get_gelombang_now(){                        
        $this->db->join('tha','tha.id_tha = gelombang.id_tha');
        $this->db->where('start_gel <= now()');
        $this->db->where('end_gel >= now()');
        $this->db->where('tha.status = 1');
        $que = $this->db->get('gelombang');
        
        return $que->row_array();
        
    }
    
    public function get_gelombang(){
        $str = "SELECT id_gel,nama_gel,start_gel,end_gel,end_bayar FROM gelombang
            LEFT JOIN tha ON gelombang.id_tha = tha.id_tha
            WHERE tha.status = 1";
        $que = $this->db->query($str);
        
        return $que->result_array();
        
    }
    
    public function get_by_id($id){
        $this->db->where('id_gel', $id);
        $que = $this->db->get('gelombang');
        
        return $que->row_array();
    }
    
    public function add($data = array()){
        $rules = array(            
            array('field' => 'nama_gel', 'label' => 'Nama Gelombang', 'rules' => 'required|trim'),
            array('field' => 'start_gel', 'label' => 'Tanggal Mulai', 'rules' => 'required|trim'),  
            array('field' => 'end_gel', 'label' => 'Tanggal Selesai', 'rules' => 'required|trim')            
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }
        
        $this->load->model('th_pmb');
        $tha = $this->th_pmb->get_tha_aktif();
        
        $data['id_tha'] = $tha['id_tha'];
        if ($this->db->insert('gelombang', $data)) {
            $this->umsg = 'Data Gelombang berhasil ditambahkan';
            return true;
        } else {
            $this->umsg = 'Data Gelombang gagal ditambahkan';
            return false;
        }
    }
    
    public function edit($id, $data = array()){
        $rules = array(            
            array('field' => 'nama_gel', 'label' => 'Nama Gelombang', 'rules' => 'required|trim'),
            array('field' => 'start_gel', 'label' => 'Tanggal Mulai', 'rules' => 'required|trim'),  
            array('field' => 'end_gel', 'label' => 'Tanggal Selesai', 'rules' => 'required|trim')            
        );

        if (!$this->set_field($rules)) {
            $this->umsg = $this->msg;
            return false;
        }
        
        $this->db->where('id_gel', $id);
        if ($this->db->update('gelombang', $data)) {
            $this->umsg = 'Data Gelombang berhasil diedit';
            return true;
        } else {
            $this->umsg = 'Data Gelombang gagal diedit';
            return false;
        }
    }
    
    public function delete($id){
        $this->db->where('id_gel',$id);
        if($this->db->delete('gelombang')){            
            return true;
        }else{
            return false;
        }
    }

}